package com.vincent.kdp.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.vincent.baseapp.util.ScreenUtil;
import com.vincent.kdp.R;

import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/6/13 0013.
 */
public class SuggestCommoditiesAdapter extends RecyclerView.Adapter<SuggestCommoditiesAdapter.ViewHolder> {


    private Context context;

    private OnCheckedItemClicker onCheckedItemClicker;
    public int newCheckedIndex = 0;

    public SuggestCommoditiesAdapter(Context context, OnCheckedItemClicker onCheckedItemClicker) {
        this.context = context;
        this.onCheckedItemClicker = onCheckedItemClicker;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
//        holder.classNameTextView.setText(studentClassList.get(position).getsClassName());
//        if (position == newCheckedIndex) {
//            holder.classNameTextView.setTextColor(context.getResources().getColor(R.color.white));
//            holder.classNameTextView.setBackgroundResource(R.drawable.bg_class_banner);
//            holder.classNameTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
////            holder.bottomLineTextView.setVisibility(View.VISIBLE);
//        } else {
////            holder.bottomLineTextView.setVisibility(View.INVISIBLE);
//            holder.classNameTextView.setBackgroundResource(android.R.color.transparent);
//            holder.classNameTextView.setTextColor(context.getResources().getColor(R.color.white));
//            holder.classNameTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
//        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rl_commodity_like, parent, false);
        ViewGroup.LayoutParams params = view.getLayoutParams();
        if (getItemCount() > 3) {
//            params.width = getScreenWidth() / 5;
        } else {
            params.width = getScreenWidth() / getItemCount();
        }
        view.setLayoutParams(params);
        return new ViewHolder(view, onCheckedItemClicker);
    }


    @Override
    public int getItemCount() {
        return 3;
    }

    public void setNewCheckedIndex(int newCheckedIndex) {
        this.newCheckedIndex = newCheckedIndex;
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private OnCheckedItemClicker onCheckedItemClicker;

        public ViewHolder(View itemView, OnCheckedItemClicker onCheckedItemClicker) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(this);
            this.onCheckedItemClicker = onCheckedItemClicker;
        }

        @Override
        public void onClick(View v) {
            if (onCheckedItemClicker != null) {
                if (getAdapterPosition() != -1) {
                    newCheckedIndex = getAdapterPosition();
                    onCheckedItemClicker.onItemChecked(v, getAdapterPosition(), getItemCount());
                }
            }
        }
    }

    private int getScreenWidth() {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.widthPixels- ScreenUtil.getInstance(context).dip2px(20);
    }

    public interface OnCheckedItemClicker {
        void onItemChecked(View view, int newPosition, int itemCount);
    }
}
